﻿Imports System.ComponentModel

Public Class SharePointLocation
    Implements INotifyPropertyChanged

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

    Private Const DefaultFolderSeparator As Char = "/"c

    Private _targetUrl As Uri
    Private _libraryId As String = String.Empty
    Private _libraryDisplayName As String = String.Empty
    Private _folderNames As String = String.Empty
    Private _folderSeparator As Char = DefaultFolderSeparator

    Private _columnId As String = String.Empty
    Private _columnDisplayName As String = String.Empty

    Public Property TargetUrl() As Uri
        Get
            Return _targetUrl
        End Get
        Set(ByVal value As Uri)
            If value <> _targetUrl Then
                _targetUrl = value
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("TargetUrl"))
            End If
        End Set
    End Property

    Public Property LibraryId() As String
        Get
            Return _libraryId
        End Get
        Set(ByVal value As String)
            If value <> _libraryId Then
                _libraryId = value
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("LibraryId"))
            End If
        End Set
    End Property

    Public Property LibraryDisplayName() As String
        Get
            Return _libraryDisplayName
        End Get
        Set(ByVal value As String)
            If value <> _libraryDisplayName Then
                _libraryDisplayName = value
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("LibraryDisplayName"))
            End If
        End Set
    End Property

    Public ReadOnly Property LibraryIdUrl() As Uri
        Get
            If String.IsNullOrEmpty(_libraryId) Then
                Return _targetUrl
            Else
                Return _targetUrl.AppendFolder(_libraryId)
            End If
        End Get
    End Property

    Public ReadOnly Property FolderIdUrl() As Uri
        Get
            If String.IsNullOrEmpty(_folderNames) Then
                Return LibraryIdUrl
            Else
                Return LibraryIdUrl.AppendFolder(_folderNames)
            End If
        End Get
    End Property

    Public Property FolderNames() As String
        Get
            Return _folderNames
        End Get
        Set(ByVal value As String)
            If value <> _folderNames Then
                _folderNames = value
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("FolderNames"))
            End If
        End Set
    End Property

    Public Property FolderSeparator() As Char
        Get
            Return _folderSeparator
        End Get
        Set(ByVal value As Char)
            If value <> _folderSeparator Then
                _folderSeparator = value
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Separator"))
            End If
        End Set
    End Property

    Public Property ColumnId() As String
        Get
            Return _columnId
        End Get
        Set(ByVal value As String)
            _columnId = value
        End Set
    End Property

    Public Property ColumnDisplayName() As String
        Get
            Return _columnDisplayName
        End Get
        Set(ByVal value As String)
            _columnDisplayName = value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Dim parent As String = String.Empty
        If _targetUrl IsNot Nothing Then
            parent = _targetUrl.AbsoluteUri
        End If
        parent = parent.Trim(DefaultFolderSeparator)
        If Not String.IsNullOrEmpty(LibraryDisplayName) Then
            parent = String.Format("{0} > {1}", parent, LibraryDisplayName)
        End If
        If Not String.IsNullOrEmpty(FolderNames) Then
            parent = String.Format("{0} > {1}", parent, String.Join(" > ", FolderNames.Split(FolderSeparator)))
        End If
        Return parent
    End Function

    Public Overloads Overrides Function Equals(obj As Object) As Boolean
        Dim other As SharePointLocation = TryCast(obj, SharePointLocation)
        If other Is Nothing Then
            Return _targetUrl Is Nothing _
                AndAlso String.IsNullOrEmpty(_libraryId) AndAlso String.IsNullOrEmpty(_libraryDisplayName) _
                AndAlso String.IsNullOrEmpty(_folderNames) AndAlso _folderSeparator = DefaultFolderSeparator
        End If
        Return _targetUrl = other._targetUrl _
            AndAlso String.Compare(_libraryId, other._libraryId, StringComparison.OrdinalIgnoreCase) = 0 _
            AndAlso String.Compare(_libraryDisplayName, other._libraryDisplayName, StringComparison.OrdinalIgnoreCase) = 0 _
            AndAlso String.Compare(_folderNames, other._folderNames, StringComparison.OrdinalIgnoreCase) = 0 _
            AndAlso String.Compare(_folderSeparator, other._folderSeparator, StringComparison.OrdinalIgnoreCase) = 0
    End Function


End Class
